package com.wm.dao.hibernate;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.wm.bean.User;
import com.wm.comm.Constant;
import com.wm.dao.UserDAO;

/**
 * @author yangbo
 *
 * 2012-12-26
 */
public class UserHibernateDAO extends HibernateDaoSupport implements UserDAO {
	
	private static final String LOAD_BY_USERNAME_USERPWD = "from User where userName = ? and userPwd = ?";
	private static final String LOAD_BY_ID = "from User where id = ?";
	private static final String LOAD_BY_USER_NAME = "from User where userName = ?";

	public List findUser() {
		return getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session s) throws HibernateException {
				Criteria c = s.createCriteria(User.class);
				c.addOrder(Order.desc("id"));
				return c.list();
			}
		});
	}

	public User findUserByUserNameUserPwd(String userName, String userPwd) {
		Object[] obj={userName,userPwd};
		List l = this.getHibernateTemplate().find(LOAD_BY_USERNAME_USERPWD,obj);
		if(l == null || l.isEmpty()){
			return null;
		}else{
			return (User) l.get(0);
		}
	}
	
	public User findUserById(long id){
		Object[] obj={new Long(id)};
		List l = this.getHibernateTemplate().find(LOAD_BY_ID,obj);
		if(l == null || l.isEmpty()){
			return null;
		}else{
			return (User) l.get(0);
		}
	}
	

	public void removeUser(User u) {
		this.getHibernateTemplate().delete(u);
	}

	public User saveUser(User u) {
		this.getHibernateTemplate().save(u);
		return u;
	}

	public User updateUser(User u) {
		this.getHibernateTemplate().update(u);
		return u;
	}
	
	public User findUserByUserName(String userName){
		List l = this.getHibernateTemplate().find(LOAD_BY_USER_NAME,userName);
		if(l == null || l.isEmpty()){
			return null;
		}else{
			return (User) l.get(0);
		}
	}
	
	public List findUsersByUserNameValidIsShow(String userName,int valid,int isShow,String orderby,int ascOrDesc,final int firstResult,final int maxResults){
		StringBuffer sb = new StringBuffer();
		sb.append("from User where 1=1");
		if(StringUtils.isNotBlank(userName)){
			sb.append(" and userName like '%");
			sb.append(userName);
			sb.append("%'");
		}
		if (valid != -1) {
			sb.append(" and valid=");
			sb.append(valid);
		}
		if (isShow != -1) {
			sb.append(" and isShow=");
			sb.append(isShow);
		}
		if (StringUtils.isNotBlank(orderby)) {
			if (ascOrDesc == Constant.ORDER_ASC) {
				sb.append(" order by " + orderby+" asc");
			}
			if (ascOrDesc == Constant.ORDER_DESC) {
				sb.append(" order by " + orderby+" desc");
			}
		}
		final String sql = sb.toString();
		return getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session s) throws HibernateException, SQLException {
				Query query = s.createQuery(sql);
				query.setFirstResult(firstResult);
				query.setMaxResults(maxResults);
				List list = query.list();
				return list;
			}
		});
	}
	
	public long getUserNumByUserNameValidIsShow(String userName,int valid,int isShow){
		StringBuffer sb = new StringBuffer();
		sb.append("select count(id) from User where 1=1");
		if(StringUtils.isNotBlank(userName)){
			sb.append(" and userName like '%");
			sb.append(userName);
			sb.append("%'");
		}
		if (valid != -1) {
			sb.append(" and valid=");
			sb.append(valid);
		}
		if (isShow != -1) {
			sb.append(" and isShow=");
			sb.append(isShow);
		}
		List l = this.getHibernateTemplate().find(sb.toString());
		
		if (l == null || l.isEmpty()) {
			return 0;
		} else {
			return (Long) l.get(0);
		}
	}
}
